<?php

class controller_content extends controller_theme
{
	public function __construct()
	{
		parent::__construct();
	}
	

	public function listing()
	{
		$category_id = get('category_id', -1, 'int');

		$model = bone::get_model('content', APP_DIR);
		$template = bone::get_template('content.listing', APP_DIR);

		$pagination = $template->get_pagination();
		$pagination->set_total($model->get_content_sum($category_id));
		$pagination->set_page(get('page', 1, 'int'));
		$pagination->set_url('./?controller=content&task=listing');

		$content_list = $model->get_content_list( $category_id );
		$category_list = $model->get_category_tree_list();
		
		$template->set_title('文章列表');
		$template->set('category_id', $category_id);
		$template->set('content_list', $content_list);
		$template->set('category_list', $category_list);
		$template->set_data($this->get_data());
		$template->display();
	}

	public function edit()
	{
		$content_id = get('content_id', 0, 'int');

		$content = bone::get_table('content');
		$content->load($content_id);

		$model = bone::get_model('content', APP_DIR);
		$category_list = $model->get_category_tree_list();

		$template = bone::get_template('content.edit', APP_DIR);
		if($content_id)
			$template->set_title('编辑文章');
		else
			$template->set_title('添加文章');

		$template->set('content_id', $content_id);
		$template->set('content', $content);
		$template->set('category_list', $category_list);
		$template->set_data($this->get_data());
		$template->display();
	}

	public function edit_save()
	{
		$id = post('id', 0, 'int');
		$my = bone::get_user();

		$content = bone::get_table('content');
		
		if( $id )
		{
			$content->load( $id );
			$content->modified_time = time();
			$content->modified_by_id = $my->id;
			$content->modified_by_name = $my->name;
			$msg = '修改文单成功';
		}
		else
		{
			$content->created_time = time();
			$content->created_by_id = $my->id;
			$content->created_by_name = $my->name;
			$msg = '添加文单成功';
		}
		$content->bind( post() );

		$content->save();

		$this->redirect('./?controller=content&task=listing&category_id='.$content->category_id, $msg);
	}


	public function delete()
	{
		$content_id = get('content_id', 0, 'int');
		if(!$content_id) exit;

		$content = bone::get_table('content');
		$content->load( $content_id );
		
		$model = bone::get_model('content', APP_DIR);
		
		if($model->delete($content_id))
			$this->redirect('./?controller=content&task=listing&category_id='.$content->category_id, '删除文章成功');
		else
			$this->redirect('./?controller=content&task=listing&category_id='.$content->category_id, $model->get_error() );
	}

	public function category_listing()
	{
	
		$model = bone::get_model('content', APP_DIR);
		$category_list = $model->get_category_tree_list();

		$template = bone::get_template('content.category_listing', APP_DIR);
		$template->set_title('分类管理');
		$template->set('category_list', $category_list);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function category_listing_save()
	{
		$ids = get('id', null, 'array(int)');
		$parent_ids = get('parent_id', 0, 'int');
		$names = get('name', '');
		if($ids)
		{
			if(is_array($ids))
			{
				for($i=0,$n=count($ids); $i<$n; $i++ )
				{
					if( !($ids[$i] || $names[$i]) ) continue;

					$table = bone::get_table('content_category');
					$table->id = $ids[$i];
					$table->parent_id = $parent_ids[$i];
					$table->name = $names[$i];
					$table->save();
				}
			}
		}

		$this->redirect('./?controller=content&task=category_listing', '保存分类成功');
	}

	
	public function ajax_delete_category()
	{

		$category_id = get('category_id', 0, 'int');
		if( ! $category_id)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{category_id}丢失');
		}
		else
		{
			$model = bone::get_model('content', APP_DIR);
			if( $model->delete_category( $category_id ) )
			{
				$this->set_ajax('status', 0);
				$this->set_ajax('description', '删除成功');
			}
			else
			{
				$this->set_ajax('status', 2);
				$this->set_ajax('description', $model->get_error());
			}
		}
		$this->ajax();
	}

	public function ajax_set_frontpage()
	{

		$content_id = get('content_id', 0, 'int');
		if( $content_id==0 )
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{frontpage_id}丢失');
		}
		else
		{
			$model = bone::get_model('content', APP_DIR);
			if( $model->set_frontpage( $content_id ) )
			{
				$this->set_ajax('status', 0);
				$this->set_ajax('description', '设置成功');
			}
			else
			{
				$this->set_ajax('status', 2);
				$this->set_ajax('description', $model->get_error());
			}
		}
		$this->ajax();
	}

	public function ajax_content_title_list()
	{
		$model = bone::get_model('content', APP_DIR);
		$content_title_list = $model->get_content_title_list();

		$content_list = array();
		foreach($content_title_list as $content)
			$content_list[$content->id] = $content->title;

		$this->set_ajax('status', 0);
		$this->set_ajax('content_list', $content_list);
		$this->ajax();
	}

	public function ajax_content_category_name_list()
	{
		$model = bone::get_model('content', APP_DIR);
		$category_list = $model->get_category_tree_list();
		$this->set_ajax('status', 0);
		$this->set_ajax('category_list', $category_list);
		$this->ajax();
	}

	
}
?>